package com.aidong.web.controller.customer;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CustomerComplaintAuditDao;
import com.aidong.dao.wills.CustomerComplaintDao;
import com.aidong.dao.wills.MemberEarnestDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.model.wills.CustomerComplaint;
import com.aidong.model.wills.CustomerComplaintAudit;
import com.aidong.model.wills.MemberEarnest;
import com.aidong.model.wills.MemberInfo;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"customerComplaintAudit"})
public class CustomerComplaintAuditController extends BaseController {
    @Resource
    CustomerComplaintDao customerComplaintDao;
    @Resource
    CustomerComplaintAuditDao customerComplaintAuditDao;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    MemberEarnestDao memberEarnestDao;

    public CustomerComplaintAuditController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("customer/audit/list");
        mv.addObject("complaintStatus", this.getTypeList("complaintStatus"));
        mv.addObject("complaintType", this.getTypeList("complaintType"));
        mv.addObject("complaintPriority", this.getTypeList("complaintPriority"));
        mv.addObject("complaintProcType", this.getTypeList("complaintProcType"));
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "title",required = false) String title, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "procType",required = false) Integer procType, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "c.shopCode", shopCode);
        cd.eq("c.type", type);
        cd.eq("c.procType", procType);
        if(!this.chkNull(status)) {
            cd.eq("c.status", status);
        } else {
            cd.gt("c.status", Integer.valueOf(0));
        }

        if(!this.chkNull(title)) {
            cd.like("title", "%" + title + "%");
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("c.complainTime", this.getDateFrom(dateFrom));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("c.complainTime", this.getDateTo(dateTo));
        }

        this.customerComplaintDao.queryPageList(pg, cd);
        Iterator mv = pg.getResultList().iterator();

        while(mv.hasNext()) {
            CustomerComplaint complaint = (CustomerComplaint)mv.next();
            if(complaint.getStatus().intValue() == 4 && "5".equals(complaint.getType())) {
                complaint.setStatus2(this.customerComplaintAuditDao.getAuditStatus5(complaint.getComplainId().intValue()));
            }
        }

        ModelAndView mv1 = new ModelAndView("customer/audit/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("id") int id) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(Integer.valueOf(id));
        if(complaint == null) {
            return this.showAlert("没有这个客诉");
        } else if(complaint.getStatus().intValue() != 1) {
            return this.showAlert("不能修改");
        } else {
            MemberInfo member = null;
            MemberEarnest earnest = null;
            if(!this.chkNull(complaint.getMemberCode())) {
                member = this.memberInfoDao.getByMemberCode(complaint.getMemberCode());
            } else if(!this.chkNullZ(complaint.getEarnestId())) {
                earnest = (MemberEarnest)this.memberEarnestDao.getById(complaint.getEarnestId());
                if(earnest != null) {
                    member = new MemberInfo(earnest.getMemberCode());
                    member.setCardCode(earnest.getCardCode());
                    member.setChName(earnest.getCustName());
                    member.setIdType(earnest.getIdType());
                    member.setIdNumber(earnest.getIdNumber());
                    member.setMobile(earnest.getMobile());
                    member.setShopCode(earnest.getShopCode());
                }
            }

            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                if(!this.chkNull(complaint.getContent())) {
                    complaint.setContent(complaint.getContent().replaceAll("\\n", "<br>"));
                }

                ArrayList picList = null;
                String[] mv;
                int len$;
                if(!this.chkNull(complaint.getPicUrls())) {
                    picList = new ArrayList();
                    String[] contractList = complaint.getPicUrls().split(",");
                    mv = contractList;
                    int arr$ = contractList.length;

                    for(len$ = 0; len$ < arr$; ++len$) {
                        String i$ = mv[len$];
                        picList.add(i$);
                    }
                }

                ArrayList var12 = null;
                if(!this.chkNull(complaint.getContractNote())) {
                    var12 = new ArrayList();
                    mv = complaint.getContractNote().split("\\|");
                    String[] var14 = mv;
                    len$ = mv.length;

                    for(int var15 = 0; var15 < len$; ++var15) {
                        String s = var14[var15];
                        var12.add(s.split(","));
                    }
                }

                ModelAndView var13 = new ModelAndView("customer/complaint/edit");
                var13.addObject("complaint", complaint);
                var13.addObject("member", member);
                if(earnest != null) {
                    var13.addObject("earnest", earnest);
                }

                var13.addObject("picList", picList);
                var13.addObject("contractList", var12);
                var13.addObject("complaintProcType", this.getTypeList("complaintProcType"));
                var13.addObject("complainReason", this.getTypeList("complainReason"));
                var13.addObject("_picPath", this.getSetting("pic.path"));
                var13.addObject("_oper", "edit");
                return var13;
            }
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("complaint") CustomerComplaint complaint) {
        if(this.chkNull(complaint.getStatus())) {
            complaint.setStatus(Integer.valueOf(1));
        }

        CustomerComplaint complaint1 = (CustomerComplaint)this.customerComplaintDao.getById(complaint.getComplainId());
        if(complaint1 == null) {
            return this.getResult(false, "没有这个客诉");
        } else if(complaint1.getStatus().intValue() != 1) {
            return this.getResult(false, "不能修改");
        } else {
            complaint1.setStatus(Integer.valueOf(2));
            complaint1.setPicUrls(complaint.getPicUrls());
            complaint1.setProcType(complaint.getProcType());
            complaint1.setComplainReason(complaint.getComplainReason());
            complaint1.setRiskMoney(complaint.getRiskMoney());
            complaint1.setUpdateTime(new Date());

            try {
                this.customerComplaintDao.update(complaint1);
                this.addUserLog("修改客诉", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
            } catch (Exception var4) {
                this.log.error("edit complaint", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"audit2"})
    public ModelAndView audit2(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit3"})
    public ModelAndView audit3(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit4"})
    public ModelAndView audit4(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit5"})
    public ModelAndView audit5(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit6"})
    public ModelAndView audit6(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit7"})
    public ModelAndView audit7(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit8"})
    public ModelAndView audit8(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"audit13"})
    public ModelAndView audit13(@RequestParam("id") int id) {
        return this.audit(id);
    }

    private ModelAndView audit(int id) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(Integer.valueOf(id));
        if(complaint == null) {
            return this.showAlert("没有这个客诉");
        } else {
            MemberInfo member = null;
            MemberEarnest earnest = null;
            if(!this.chkNull(complaint.getMemberCode())) {
                member = this.memberInfoDao.getByMemberCode(complaint.getMemberCode());
            } else if(!this.chkNullZ(complaint.getEarnestId())) {
                earnest = (MemberEarnest)this.memberEarnestDao.getById(complaint.getEarnestId());
                if(earnest != null) {
                    member = new MemberInfo(earnest.getMemberCode());
                    member.setCardCode(earnest.getCardCode());
                    member.setChName(earnest.getCustName());
                    member.setIdType(earnest.getIdType());
                    member.setIdNumber(earnest.getIdNumber());
                    member.setMobile(earnest.getMobile());
                    member.setShopCode(earnest.getShopCode());
                }
            }

            if(member == null) {
                return this.showAlert("会员信息错误");
            } else {
                if(!this.chkNull(complaint.getContent())) {
                    complaint.setContent(complaint.getContent().replaceAll("\\n", "<br>"));
                }

                ArrayList picList = null;
                String[] auditList;
                int len$;
                if(!this.chkNull(complaint.getPicUrls())) {
                    picList = new ArrayList();
                    String[] contractList = complaint.getPicUrls().split(",");
                    auditList = contractList;
                    int mv = contractList.length;

                    for(len$ = 0; len$ < mv; ++len$) {
                        String i$ = auditList[len$];
                        picList.add(i$);
                    }
                }

                ArrayList var12 = null;
                if(!this.chkNull(complaint.getContractNote())) {
                    var12 = new ArrayList();
                    auditList = complaint.getContractNote().split("\\|");
                    String[] var14 = auditList;
                    len$ = auditList.length;

                    for(int var16 = 0; var16 < len$; ++var16) {
                        String s = var14[var16];
                        var12.add(s.split(","));
                    }
                }

                List var13 = this.customerComplaintAuditDao.getListByComplaintId(id);
                ModelAndView var15 = new ModelAndView("customer/audit/" + (complaint.getStatus().intValue() == 9?"proc":(complaint.getStatus().intValue() == 12?"adrec":"audit")));
                var15.addObject("complaint", complaint);
                var15.addObject("member", member);
                if(earnest != null) {
                    var15.addObject("earnest", earnest);
                }

                var15.addObject("picList", picList);
                var15.addObject("contractList", var12);
                var15.addObject("auditList", var13);
                var15.addObject("complainReason", this.getTypeList("complainReason"));
                var15.addObject("_picPath", this.getSetting("pic.path"));
                var15.addObject("_oper", "edit");
                return var15;
            }
        }
    }

    @RequestMapping({"_audit2"})
    @ResponseBody
    public String _audit2(@ModelAttribute("audit") CustomerComplaintAudit audit) {
        return this._audit(audit, 2);
    }

    @RequestMapping({"_audit3"})
    @ResponseBody
    public String _audit3(@ModelAttribute("audit") CustomerComplaintAudit audit) {
        return this._audit(audit, 3);
    }

    @RequestMapping({"_audit4"})
    @ResponseBody
    public String _audit4(@ModelAttribute("audit") CustomerComplaintAudit audit) {
        return this._audit(audit, 4);
    }

    @RequestMapping({"_audit5"})
    @ResponseBody
    public String _audit5(@ModelAttribute("audit") CustomerComplaintAudit audit, @RequestParam(value = "complainReason",required = false) String complainReason, @RequestParam(value = "riskMoney",required = false) String riskMoney) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(audit.getComplainId());
        if(complaint != null && !this.chkNull(complainReason) && riskMoney != null && (!complainReason.equalsIgnoreCase(complaint.getComplainReason()) || !riskMoney.equalsIgnoreCase(complaint.getRiskMoney()))) {
            complaint.setComplainReason(complainReason);
            complaint.setRiskMoney(riskMoney);
            complaint.setUpdateTime(new Date());
            this.customerComplaintDao.update(complaint);
        }

        return this._audit(audit, 5);
    }

    @RequestMapping({"_audit6"})
    @ResponseBody
    public String _audit6(@ModelAttribute("audit") CustomerComplaintAudit audit, @RequestParam(value = "complainReason",required = false) String complainReason, @RequestParam(value = "riskMoney",required = false) String riskMoney) {
        CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(audit.getComplainId());
        if(complaint != null && !this.chkNull(complainReason) && riskMoney != null && (!complainReason.equalsIgnoreCase(complaint.getComplainReason()) || !riskMoney.equalsIgnoreCase(complaint.getRiskMoney()))) {
            complaint.setComplainReason(complainReason);
            complaint.setRiskMoney(riskMoney);
            complaint.setUpdateTime(new Date());
            this.customerComplaintDao.update(complaint);
        }

        return this._audit(audit, 6);
    }

    @RequestMapping({"_audit7"})
    @ResponseBody
    public String _audit7(@ModelAttribute("audit") CustomerComplaintAudit audit) {
        return this._audit(audit, 7);
    }

    @RequestMapping({"_audit8"})
    @ResponseBody
    public String _audit8(@ModelAttribute("audit") CustomerComplaintAudit audit) {
        return this._audit(audit, 8);
    }

    @RequestMapping({"_audit13"})
    @ResponseBody
    public String _audit13(@ModelAttribute("audit") CustomerComplaintAudit audit) {
        return this._audit(audit, 13);
    }

    private String _audit(CustomerComplaintAudit audit, int st) {
        if(this.chkNull(audit.getComplainId())) {
            return this.getResult(false, "没有这个客诉");
        } else {
            CustomerComplaint complaint = (CustomerComplaint)this.customerComplaintDao.getById(audit.getComplainId());
            if(complaint == null) {
                return this.getResult(false, "没有这个客诉");
            } else if(complaint.getStatus().intValue() != st) {
                return this.getResult(false, "不能审批");
            } else {
                if(this.chkNull(audit.getAuditAdd())) {
                    audit.setAuditAdd(Integer.valueOf(0));
                }

                audit.setAuditType(Integer.valueOf(st));
                audit.setAuditUser(this.getLoginUserCode());
                audit.setAuditTime(new Date());

                try {
                    this.customerComplaintAuditDao.saveAudit(audit);
                    this.addUserLog("客诉审核", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
                } catch (Exception var5) {
                    this.log.error("audit complaint", var5);
                    return this.getResult(false, "审批发生错误");
                }

                return this.getResult(true, "审批完成");
            }
        }
    }

    @RequestMapping({"proc"})
    public ModelAndView proc(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"_proc"})
    @ResponseBody
    public String _proc(@ModelAttribute("complaint") CustomerComplaint complaint) {
        if(this.chkNull(complaint.getProcResult())) {
            return this.getResult(false, "请输入处理结果");
        } else {
            CustomerComplaint complaint1 = (CustomerComplaint)this.customerComplaintDao.getById(complaint.getComplainId());
            if(complaint1 == null) {
                return this.getResult(false, "没有这个客诉");
            } else if(complaint1.getStatus().intValue() != 9) {
                return this.getResult(false, "不能处理");
            } else {
                complaint1.setProcResult(complaint.getProcResult());
                complaint1.setProcNote(complaint.getProcNote());
                complaint1.setProcUser(this.getLoginUserCode());
                complaint1.setProcTime(new Date());
                complaint1.setStatus(Integer.valueOf(10));
                complaint1.setUpdateTime(new Date());

                try {
                    this.customerComplaintDao.update(complaint1);
                    this.addUserLog("处理客诉", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
                } catch (Exception var4) {
                    this.log.error("proc complaint", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"adrec"})
    public ModelAndView adrec(@RequestParam("id") int id) {
        return this.audit(id);
    }

    @RequestMapping({"_adrec"})
    @ResponseBody
    public String _adrec(@ModelAttribute("complaint") CustomerComplaint complaint) {
        if(this.chkNull(complaint.getReceiptInfo())) {
            return this.getResult(false, "请输入收款信息");
        } else {
            CustomerComplaint complaint1 = (CustomerComplaint)this.customerComplaintDao.getById(complaint.getComplainId());
            if(complaint1 == null) {
                return this.getResult(false, "没有这个客诉");
            } else if(complaint1.getStatus().intValue() != 12) {
                return this.getResult(false, "不能处理");
            } else {
                complaint1.setReceiptInfo(complaint.getReceiptInfo());
                complaint1.setStatus(Integer.valueOf(13));
                complaint1.setUpdateTime(new Date());

                try {
                    this.customerComplaintDao.update(complaint1);
                    this.addUserLog("客诉营运补充收款信息", "编号:" + complaint.getComplainId() + ",标题:" + complaint.getTitle());
                } catch (Exception var4) {
                    this.log.error("adrec complaint", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }
}